package example.dao.openjpa;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EntityContext;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import example.dao.IAccountDao;
import example.dao.PrimaryKeyGenerator;
import example.model.Account;


public class AccountJPADao implements IAccountDao {

	
	private EntityManager em;

	
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	/* (non-Javadoc)
	 * @see example.dao.openjpa.IAccountDao#create(example.model.Account)
	 */
	public void create(Account account) {
		if (account.getPrimaryKey()==null) {
			String pk = PrimaryKeyGenerator.getUUID();
			account.setPrimaryKey(pk);
		}
		AccountEntity entityAccount = fromModelToEntity(account);
		
		em.persist(entityAccount);		
	}

	/* (non-Javadoc)
	 * @see example.dao.openjpa.IAccountDao#update(example.model.Account)
	 */
	public void update(Account account) {
		String pk = account.getPrimaryKey();
		
		AccountEntity entityAccount = em.find(AccountEntity.class, pk);
		// Move all in JPAAccount
		entityAccount.setAccno(account.getAccno());
		entityAccount.setAccountType(account.getAccountType());
		entityAccount.setBalance(account.getBalance());
		entityAccount.setDateOpen(account.getDateOpen());
		entityAccount.setOwner(account.getOwner());
		entityAccount.setActive(account.isActive());
		
		em.persist(entityAccount);

		
	}
	
	/* (non-Javadoc)
	 * @see example.dao.openjpa.IAccountDao#delete(java.lang.String)
	 */
	public void remove(String pk) {
		AccountEntity entityAccount = em.find(AccountEntity.class, pk);
		
		em.remove(entityAccount);

	}

	/* (non-Javadoc)
	 * @see example.dao.openjpa.IAccountDao#retrieve(java.lang.String)
	 */
	public Account retrieve(String pk) {
		AccountEntity entityAccount = em.find(AccountEntity.class, pk);		
		Account account = fromEntityToModel(entityAccount);		
		return account;
	}
	


	public List<Account> selectAll() {
		Query q = em.createQuery("SELECT x FROM example.dao.openjpa.AccountEntity x");
		List<AccountEntity> results = q.getResultList();
		List<Account> accounts = new ArrayList<Account>();
		for (AccountEntity entity: results) {
			accounts.add(fromEntityToModel(entity));
		}		
		return accounts;
	}	
	
	
	private static AccountEntity fromModelToEntity(Account account) {
		AccountEntity entityAccount = null;
		if (account!=null) {
			entityAccount = new AccountEntity();
			
			entityAccount.setAccno(account.getAccno());
			entityAccount.setAccountType(account.getAccountType());
			entityAccount.setBalance(account.getBalance());
			entityAccount.setDateOpen(account.getDateOpen());
			entityAccount.setOwner(account.getOwner());
			entityAccount.setActive(account.isActive());
		}		
		return entityAccount;
	}
	
	private static Account fromEntityToModel(AccountEntity entityAccount) {
		Account account = null;
		if (entityAccount!=null) {
			account = new Account();
			
			account.setAccno(entityAccount.getAccno());
			account.setAccountType(entityAccount.getAccountType());
			account.setBalance(entityAccount.getBalance());
			account.setDateOpen(entityAccount.getDateOpen());
			account.setOwner(entityAccount.getOwner());
			account.setActive(entityAccount.isActive());
		}
		return account;
	}

}
